<template>
	<view class="orderlist">
		<view class="tabsBox">
			<view v-if="identity == 1" class="title">
				我的订单
			</view>
			<view v-if="identity == 0" class="" style="display: flex;align-items: center;">
				<view class="">
					中牟订单
				</view>
				<image class="" src="@/static/image/bottoms.png"
					style="width: 40rpx;height: 40rpx;margin-left: 10rpx;" />
			</view>
			<view class="tabs">
				<view class="tabsitem" @click="handleTabs(0)">
					<image v-if="currIndex == 0" class="" src="@/static/image/alled.png"
						style="width: 48rpx;height: 48rpx;" />
					<image v-else class="" src="@/static/image/all.png" style="width: 48rpx;height: 48rpx;" />
					<view class="" :class="{colored: currIndex == 0}">
						全部
					</view>
				</view>
				<!-- <view class="tabsitem" @click="handleTabs(1)">
					<image v-if="currIndex == 1" class="" src="@/static/image/bdzed.png"
						style="width: 48rpx;height: 48rpx;" />
					<image v-else class="" src="@/static/image/bdz.png" style="width: 48rpx;height: 48rpx;" />
					<view class="" :class="{colored: currIndex == 1}">
						待支付
					</view>
				</view> -->
				<view class="tabsitem" @click="handleTabs(2)">
					<image v-if="currIndex == 2" class="" src="@/static/image/dsd.png"
						style="width: 48rpx;height: 48rpx;" />
					<image v-else class="" src="@/static/image/dsded.png" style="width: 48rpx;height: 48rpx;" />
					<view class="" :class="{colored: currIndex == 2}">
						已支付
					</view>
				</view>
				
				<view class="tabsitem" @click="handleTabs(3)">
					<image v-if="currIndex == 3" class="" src="@/static/image/ysded.png"
						style="width: 48rpx;height: 48rpx;" />
					<image v-else class="" src="@/static/image/ysd.png" style="width: 48rpx;height: 48rpx;" />
					<view class="" :class="{colored: currIndex == 3}">
						待送单
					</view>
				</view>
				<view class="tabsitem" @click="handleTabs(4)">
					<image v-if="currIndex == 4" class="" src="@/static/image/yhked.png"
						style="width: 48rpx;height: 48rpx;" />
					<image v-else class="" src="@/static/image/yhk.png" style="width: 48rpx;height: 48rpx;" />
					<view class="" :class="{colored: currIndex == 4}">
						已送单
					</view>
				</view>
			</view>

		</view>


		<view class="dataList">
			<view v-if="dataList.length>0" class="">

				<view class="outbox" v-for="(item,index) in dataList">
					<view class="orderNumber">
						<view class="">
							手机号:
						</view>
						<view class="">
							{{item.mobile}}
						</view>
					</view>
					<view class="contentBox">
						<view class="innerBox">
							<view class="info">
								<view class="avatar">
									<image class="" src="@/static/image/lovespecialist.png"
										style="width: 45.55rpx;height: 45.55rpx;" />
								</view>
								<view class="nikename">
									{{item.name}}
								</view>
							</view>

							<view class="message">
								<view class="name">
									{{item.good.name}}
								</view>
								<!-- <view class="shuliang">
									{{item.good_info.capacity}}L
								</view> -->
							</view>
							<view class="sumandnum">
								<view class="sum">
									￥{{item.good.price}}
								</view>
								<view class="num" style="color: #A7A7A7;">
									x{{item.count}}
								</view>
							</view>
						</view>

						<view class="dfk">
							<!-- <view v-if="item.status == 0" class="">
								待收款：￥{{item.good.price*item.count}}
							</view> -->
							<view v-if="item.status == 0" class="status">
								订单状态：待支付
							</view>
							<view v-if="item.status == 1" class="status">
								订单状态：已支付
							</view>
							<view v-if="item.status == 2" class="status">
								订单状态：待送单
							</view>
							<view v-if="item.status == 3" class="status" style="color: red;">
								订单状态：已送单
							</view>
						</view>
					</view>
				</view>
			</view>

			<view v-else class="">
				<u-empty text="暂无订单" icon="http://cdn.uviewui.com/uview/empty/order.png">
				</u-empty>
			</view>
		</view>

		<view class="" style="height: 200rpx;"></view>


	<!-- 	<view v-if="currIndex == 1 && dataList.length > 0" class="bottomContent">
			<view class="left">
				<view class="">
					共计{{dataList.length}}单，
				</view>
				<view class="">
					合计: <text class="" style="color: #FF4E4E; font-size: 40rpx;">{{unit}}件</text>
				</view>
			</view>
			<view class="right" @click="handleopendeclarationFlag">
				立即报单
			</view>
		</view> -->


		<u-popup :show="show" mode="center">
			<view class="subAddress">
				<view class="title">
					确认付款
				</view>
				<view class="content">
					您确定付款该订单吗？
				</view>


				<view class="bottomBtn">
					<view class="btnleft" @click="show = false">
						取消
					</view>
					<view class="btnright" @click="handleShowpayment">
						确认
					</view>
				</view>
			</view>
		</u-popup>


		<u-popup :show="declarationFlag" mode="center">
			<view class="subAddress">
				<view class="title">
					确认报单
				</view>
				<view class="content">
					您确定要立即报单吗？
				</view>

				<view class="bottomBtn">
					<view class="btnleft" @click="declarationFlag = false">
						取消
					</view>
					<view class="btnright" @click="handledeclarationFlag">
						确认
					</view>
				</view>
			</view>
		</u-popup>

	</view>
</template>

<script>
	import {
		submitorderList,
		createorder,
		orderList,
		payment,
		salerList
	} from "@/api/api.js"
	export default {
		data() {
			return {
				currIndex: 0,
				show: false,
				identity: null,
				declarationFlag: false,
				dataList: [],
				unit: null,
				baodanFlag: true,
				baodanLenght: 0,
				orderId: null,
			}
		},
		onLoad() {
			this.identity = uni.getStorageSync("identity")
			this.getorderList()
		},
		methods: {

			async getSubmitorderList() {
				let res = await submitorderList()
				console.log("报单列表", res)
				this.dataList = []

				res.data.data.forEach((item, index) => {
					if (item.status == 0) {
						this.dataList.push(item)
						this.unit += item.count
						this.baodanLenght++
					}

				})

			},
			async getorderList() {
				let res = await orderList()
				this.dataList = []

				res.data.data.forEach((item, index) => {
					// let sum = 0
					// item.pre_orders.forEach((subitem, subindex) => {
					// 	sum += (subitem.count * subitem.good_info.price)
					// })
					// item.sumPrice = sum
					// console.log("status", this.currIndex, item.status)
					if (this.currIndex == 0) {
						this.dataList.push(item)
						console.log("0000000000000000000000")
					}else{
						if ((this.currIndex-1) == item.status) {
							this.dataList.push(item)
						}
					}


					// if (this.currIndex == 1) {
					// 	if (item.status == 0) {
					// 		this.dataList.push(item)
					// 		this.unit += item.count
					// 		this.baodanLenght++
					// 	}
					// }


					// if (this.currIndex == 2) {
					// 	if (item.status == 1) {
					// 		this.dataList.push(item)
					// 	}
					// }
					// if (this.currIndex == 3) {
					// 	if (item.status == 2) {
					// 		this.dataList.push(item)
					// 	}
					// }
					// if (this.currIndex == 4 && this.identity == 1) {
					// 	if (item.status == 3) {
					// 		this.dataList.push(item)
					// 	}
					// }
					// if (this.currIndex == 4 && this.identity == 0) {
					// 	if (item.status == 4) {
					// 		this.dataList.push(item)
					// 	}
					// }

				})
				console.log("订单列表", this.dataList)
			},
			handleTabs(index) {
				this.currIndex = index
				this.dataList = []
				this.getorderList()
			},
			handleOpenShow(id) {
				console.log("444444444", id)
				this.show = true
				this.orderId = id
			},
			async handleShowpayment() {
				let that = this
				let res = await payment({
					out_trade_no: that.orderId
				})
				console.log("支付信息", res)
				if (res.code == 1000) {
					uni.requestPayment({
						provider: 'wxpay',
						appId: res.data.result.appId,
						timeStamp: res.data.result.timeStamp,
						nonceStr: res.data.result.nonceStr,
						package: res.data.result.package,
						signType: res.data.result.signType,
						paySign: res.data.result.paySign,
						success(res) {
							console.log("支付成功、、、、、、、、、、、、、、、、")
							uni.showToast({
								title: '支付成功！',
								icon: "none",
								duration: 2000
							});
							that.getorderList()
							that.show = false
						},
						fail(e) {}
					})
				} else {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 2000
					});
				}
			},
			handleopendeclarationFlag() {
				this.declarationFlag = true
			},
			async handledeclarationFlag() {
				let res = await createorder()
				if (res.code == 1000) {
					uni.showToast({
						title: '报单成功！',
						icon: "none",
						duration: 1000
					});
					this.declarationFlag = false
					this.getSubmitorderList()

				}
			},

		}
	}
</script>

<style scoped lang="scss">
	.subAddress {
		width: 686rpx;
		// height: 500rpx;
		background: #FFFFFF;
		// box-shadow: 0rpx 8rpx 52rpx 0rpx rgba(0, 0, 0, 0.12);
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		padding: 90rpx 30rpx;
		box-sizing: border-box;

		.title {
			font-size: 50rpx;
			font-family: PingFang SC-Semibold, PingFang SC;
			font-weight: 600;
			color: #000000;
			line-height: 50rpx;
			text-align: center;
		}

		.content {
			margin-top: 50rpx;
			font-size: 50rpx;
			font-family: PingFang SC-Semibold, PingFang SC;
			font-weight: 600;
			color: #000000;
			line-height: 50rpx;
			text-align: center;
		}

		.bottomBtn {
			display: flex;
			justify-content: space-between;
			margin-top: 84rpx;

			.btnleft {
				width: 184rpx;
				height: 88rpx;
				background: #A7A7A7;
				border-radius: 60rpx 60rpx 60rpx 60rpx;
				font-size: 48rpx;
				font-family: PingFang SC-Semibold, PingFang SC;
				font-weight: 600;
				color: #FFFFFF;
				line-height: 50rpx;
				text-align: center;
				line-height: 88rpx;
			}

			.btnright {
				width: 406rpx;
				height: 88rpx;
				background: linear-gradient(180deg, #FE5C5C 0%, rgba(255, 78, 78, 0.67) 100%);
				border-radius: 60rpx 60rpx 60rpx 60rpx;
				font-size: 48rpx;
				font-family: PingFang SC-Semibold, PingFang SC;
				font-weight: 600;
				color: #FFFFFF;
				line-height: 50rpx;
				text-align: center;
				line-height: 88rpx;
			}
		}
	}

	.orderlist {
		.tabsBox {
			width: 702rpx;
			height: 250rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 0rpx 8rpx 2rpx rgba(0, 0, 0, 0.05);
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			margin: 24rpx auto;
			padding: 16rpx;
			box-sizing: border-box;

			.title {
				font-size: 32rpx;
				font-family: PingFang SC-Semibold, PingFang SC;
				font-weight: 600;
				color: #000000;
				line-height: 50rpx;
			}

			.tabs {
				display: flex;
				width: 100%;
				padding: 30rpx;
				box-sizing: border-box;
				justify-content: space-between;
				font-size: 28rpx;
				font-family: PingFang SC-Semibold, PingFang SC;
				font-weight: 600;
				color: #000000;
				line-height: 50rpx;

				.tabsitem {
					text-align: center;
				}
			}

			.colored {
				color: #FF4E4E;
			}
		}

		.dataList {


			.outbox {
				width: 702rpx;
				background: #FFFFFF;
				box-shadow: 0rpx 0rpx 8rpx 0rpx rgba(0, 0, 0, 0.05);
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				margin: 0 auto;
				padding: 16rpx 24rpx;
				box-sizing: border-box;
				margin-bottom: 24rpx;

				.orderNumber {
					font-size: 24rpx;
					font-family: PingFang SC-Semibold, PingFang SC;
					font-weight: 600;
					color: #000000;
					line-height: 50rpx;
					display: flex;

					.status {
						display: flex;
						margin-left: 180rpx;
					}

					.payment {
						font-size: 24rpx;
						font-family: PingFang SC-Semibold, PingFang SC;
						font-weight: 600;
						color: #FF4E4E;
						line-height: 50rpx;
						display: flex;
						margin-left: 100rpx;

						.paymentbtn {
							width: 120rpx;
							height: 50rpx;
							background: #FF4E4E;
							border-radius: 16rpx 16rpx 16rpx 16rpx;
							font-size: 24rpx;
							font-family: PingFang SC-Semibold, PingFang SC;
							font-weight: 600;
							color: #FFFFFF;
							line-height: 50rpx;
							text-align: center;
						}
					}

				}

				.driver {
					font-size: 24rpx;
					font-family: PingFang SC-Semibold, PingFang SC;
					font-weight: 600;
					color: #000000;
					line-height: 50rpx;
					display: flex;
				}

				.contentBox {
					width: 666rpx;
					background: #FFFFFF;
					box-shadow: 0rpx 0rpx 8rpx 2rpx rgba(0, 0, 0, 0.05);
					border-radius: 16rpx 16rpx 16rpx 16rpx;
					padding: 18rpx 16rpx;
					box-sizing: border-box;
					margin-bottom: 16rpx;
					margin-top: 10rpx;

					.innerBox {
						display: flex;
						justify-content: space-between;

						.info {
							display: flex;
							font-size: 28rpx;
							font-family: PingFang SC-Semibold, PingFang SC;
							font-weight: 600;
							color: #000000;
							line-height: 50rpx;
						}

						.message {
							.name {
								width: 320rpx;
								height: 50rpx;
								font-size: 24rpx;
								font-family: PingFang SC-Regular, PingFang SC;
								font-weight: 400;
								color: #000000;
								line-height: 50rpx;
								white-space: nowrap;
								overflow: hidden;
								text-overflow: ellipsis;
							}

							.shuliang {
								font-size: 24rpx;
								font-family: PingFang SC-Regular, PingFang SC;
								font-weight: 400;
								color: #A7A7A7;
								line-height: 50rpx;
							}

						}

						.sumandnum {
							font-size: 24rpx;
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #000000;
							line-height: 50rpx;
							text-align: right;
						}
					}

					.dfk {
						font-size: 28rpx;
						font-family: PingFang SC-Semibold, PingFang SC;
						font-weight: 600;
						color: #000000;
						line-height: 50rpx;
						// text-align: right;
						display: flex;
						justify-content: space-between;
					}
				}
			}
		}
	}

	.bottomContent {
		width: 100%;
		background: #FFFFFF;
		position: fixed;
		bottom: 0;
		padding: 20rpx 40rpx;
		box-sizing: border-box;
		padding-bottom: constant(safe-area-inset-bottom);
		/* 兼容 iOS 设备 */
		padding-bottom: env(safe-area-inset-bottom);
		display: flex;
		justify-content: space-between;

		.left {
			font-size: 40rpx;
			font-family: PingFang SC-Semibold, PingFang SC;
			font-weight: 600;
			color: #A7A7A7;
			line-height: 50rpx;
			display: flex;
		}

		.right {
			width: 224rpx;
			height: 90rpx;
			background: linear-gradient(180deg, #FE5C5C 0%, rgba(255, 78, 78, 0.67) 100%);
			border-radius: 60rpx 60rpx 60rpx 60rpx;
			font-size: 40rpx;
			font-family: PingFang SC-Semibold, PingFang SC;
			font-weight: 600;
			color: #FFFFFF;
			line-height: 90rpx;
			text-align: center;
		}
	}
</style>